home *** CD-ROM | disk | FTP | other *** search
- 0660103030566
- 1
- 2Page #
- 9[....................................................]
- Ç
- ====================SECONDLESSONIN====================
-
- àPASCAL
- Ç
- =================BySERGEVAILLANCOURT==================
-
-
- Hello,everyone!!
-
- I'mhappytobebackwithyoutogiveyouparttwoofthis
- Pascaltutorial.Ihopethefirstpartwasusefulandthatyour
- firstprogramsworkedsuccessfully.Mostimportantly,don'tforget
- toleavemeamessageontheFASTERBBSifyouhaveanyproblems.
- Itwillbeapleasureformetohelpyouifpossible.
-
- But,enoughidlechattingandlet'sgetstartedwiththe
- matrixvariables.
-
-
- üMATRIXVARIABLES
- Ç================
-
- Amatrixvariableisavariablecontainingmanyvalues,each
- oneofthesebeingindexed.
-
- Supposewewanttosavesomestudentsgrades.Afterhaving
- learnedlesson1,weshouldwritethefollowingprogram:
-
- àPROGRAMÇscoreà(INPUT);Ç
- (*rememberthatwordsinCapitals/ItalicsarePascal
- reservedwords*)
- àVARÇn1,n2,n3,n4,n5,n6,n7.n8:àINTEGER;Ç
- (*Weassumethestudenthas8scorestograde*)
- àBEGINÇ
- àREADLNÇ(n1,n2,n3,n4,n5,n6,n7,n8);
- àEND.Ç
-
- (program8)
-
- Buttheprogramwouldbemuchsimplerwithamatrixvariable:
-
- àPROGRAMÇscore2à(INPUT);äÇ
- àVARÇscores:àARRAYäÇ[1..8]àOFINTEGER;Ç
- (*declarationofamatrixtypevariable*)
- i:àINTEGER;Ç(*loopingindex*)
- àBEGINÇ
- àFORÇi:=1àTOÇ8àDOREADLNÇ(scores[i]);
- àEND.Ç
-
- (program9)
-
- YouwillnoticethattheàFORÇloopindexalsobecomesthe
- matrixvariable'sindex.
-
- Declaringamatrixvariablemustalwaysadheretothe
- followingsyntax:
-
- àARRAYÇ
- |
- à[Ç
- Sizeofthematrix(typeofindex)
- à]Ç
- |
- àOFÇ
- |
- Typeofmatrixvariable
-
- (figure9)
-
- Theindexcarriesthesizeofthematrix.Inthepreceding
- example,thematrixhas8slots,numberedfrom1to8.Thisindex
- hastobeoneofthefollowingtypes:
-
- -SCALAR(seebelow)
- -CHARACTER(àCHARÇ)
- -BOOLEAN(àBOOLEANÇ)
- -INTERVAL
-
- TheINTERVALtypeis,infact,a"sub-type",likeinour
- example,1..8isanintervaloftypeàINTEGERÇ.It'sthemost
- oftenusedforindexingamatrix.Thedeclarationofaninterval
- typehasthefollowingaspect.
-
- constant
- |
- ..
- |
- constant
-
- (figure10)
-
- Understandherethatanintervalofrealnumbersisnot
- acceptablebecausebetweentwogivennumbers,theremaybean
- infinityofothernumbers.
-
- Constantsmaybedefinedintheconstantsdeclaration.
- Example:
-
- àPROGRAMÇscore3à(INPUT);
- CONSTÇmaxscore=8;
- àVARÇscore:àARRAYÇ[1..maxscore]àOFINTEGER;
- Ç i:àINTEGER;
- BEGIN
- FORÇi:=1àTOÇmaxscoreàDOREADLNÇ(score[i]);
- àEND.Ç
-
- (program10)
-
- Wemayalsodeclarethetypeofindex:
-
- àPROGRAMÇscore4à(INPUT);
- CONSTÇmaxscore=8;
- àTYPEÇrange:1..maxscore;
- àVARÇscore:àARRAYÇ[range]àOFINTEGER;Ç
- i:àINTEGER;Ç
- àBEGINÇ
- àFORÇi:=1àTOÇmaxscoreàDOREADLNÇ(score[i]);
- àEND.Ç
-
- (program11)
-
- Thisway,ifduringthenextsession,thescoreofeach
- studentneedstobecompiledonatotalof7insteadof8,the
- onlythingyouwouldneedtodoistochangethevalueof
- maxscore.
-
- Asforthematrix,itmaybeofanytype.Thisbecomes
- interestingifourvariable,insteadofbeingassignedtoonlyone
- student,shouldcontainthescoresofthewholeclass.Thus,the
- matrix'stypewouldbeanothermatrix.
-
- àPROGRAMÇclassà(INPUT);
- CONSTÇmaxscore=8;(*numberofscoresperstudent*)
- maxstudent=35;(*numberofstudentsperclass*)
- àTYPEÇscorerange:1..maxscore;
- studentrange:1..maxstudent;
- onestudent:àARRAYÇ[scorerange]àOFINTEGER;äÇ
- class:àARRAYÇ[studentrange]àOFÇonestudent;
- àVARÇscore:class;(*2dimensionvariableoftypeclass*)
- i,j:àINTEGER;Ç(*loopindex*)
- àBEGINÇ
- àFORÇi:=1àTOÇmaxstudentàDOÇ
- àFORÇj:=1àTOÇmaxscoreàDOÇ
- àREADLNÇ(score[i,j]);
- àEND.Ç
-
- à Ç(program12)
-
- Wehadinfrontofus,inthelastprogram,atwo-dimension
- array.
-
- "àARRAYÇ[type1]àOFARRAYÇ[type2]àOFÇtype3"isthesame
- thingas"àARRAYÇ[type1,type2]àOFÇtype3".
-
- WeunderstandherethatinàREADLNÇ(score[i,j]),the"i"is
- thefirstindex(firstdimension)whilethe"j"isthesecond
- index(seconddimension).Therearenolimitstothenumberof
- dimensions(1,2,3,4,etc...)Onlyyourcompileroryourcomputer's
- availablememorywillphysicallylimityou.However,itisrare
- indeedtoneedmorethanthreedimensions.
-
- üCHARACTERSTRINGS
- Ç=================
-
- Acharacterstringisinfactnothingmorethanamatrixof
- characters.Example:
-
- àARRAYÇ[1..30]àOFCHARÇ
-
- Inthisexample,weobtaina30characterstring.
-
- àPROGRAMÇyournameà(INPUT,OUTPUT);
- CONSTÇstringlenght=30;
- (*maximumlenghtofthecharacterstring*)
- àTYPEÇstring30:àARRAYÇ[1..stringlenght]àOFCHAR;
- VARÇname:string30;
- àBEGINÇ
- àWRITELNÇ('What'syourname?')
- àREADLNÇ(name);
- à WRITELNÇ('Goodday',name);
- à END.
- Ç
- (program13)
-
-
- Insidethecomputer,onecharacteroccupiesonebyteof
- memory.Thus,inthelastexample,thestringrequires30bytesof
- storage.However,it'spossibletoreleasealmosthalfthememory
- withtheàPACKÇinstruction.
-
- Thisinstructionallowsustostoreeachcharacterinone
- nybbleinsteadofabyte,whichsavesalotofmemoryinthe
- treatmentofacharactermatrix.Tobetterunderstand,readthe
- followingprogramwhichfetchesallthestudentsscoreswhich
- namesappearinsuccessiononthescreen.
-
- àPROGRAMÇclass2à(INPUT,OUTPUT);
- Ç àCONSTÇmaxscore=8;
- maxstudent=35;
- àTYPEÇscorerange:1..maxscore;
- studentrange:1..maxstudent;
- string30:àPACKEDARRAYÇ[1..30]àOFCHAR;
- Ç onestudent:àARRAYÇ[studentrange]àOFINTEGER;
- Ç àVARÇscores:àARRAYÇ[studentrange]àOFINTEGER;Ç
- (*matrixofscores*)
- names:àARRAYÇ[studentrange]àOFÇstring30;
- (*matrixofnames*)
- i,j:àINTEGER;
- Ç àBEGINÇ
- àFORÇi:=1àTOÇmaxstudentàDOÇ
- àBEGIN
- WRITEÇ(names[i]);(*writesthename*)
- àFORÇj:=1àTOÇmaxscoreàDOÇ
- àREADÇ(scores[i,j]);
- (*readsthestudentsscores*)
- àWRITELN;Ç(*returnsthecursortothenextline*)
- àEND;Ç(*fori*)
- à END.
- Ç
- (program14)
-
- àPACKÇandàUNPACKÇ
- ================
-
- Asmentionedabove,àPACKäÇallowsyoutocompressthecontents
- ofamatrix.Notonlyofacharactermatrixbutalsoothertypes.
- Infactanoncompactedmatrixreservesatleastonebyteper
- entry,dependingonthetypeofmatrix.Ifitisamatrixof
- booleanvalues,itwillreserveonebyteperentrywhileonlythe
- firstbitofthebyteissignificant.Ifwepackthismatrix,we
- willbeabletostore8valuesintoasinglebyte,asavingof
- approximately90%ofmemory.
-
- àUNPACKÇhastheoppositeeffect,i.e.bringsvaluesbackto
- theiroriginalstate.
-
- Topackamatrixofvalues,usethefollowingsyntax:
-
- àPACKÇ(a,b,c);
-
- Where"a"isthematrixvariable;
- "b"istheresult(compactedmatrixvariable);
- "c"isanintegerholdingtheindexofthefirst
- componentof"a"tooccurintheoperation.
-
- Tounpackamatrix,youmustusethefollowingsyntax:
-
- à UNPACKÇ(c,a,b);
-
- Where"a","b",and"c"carrythesamemeaningasinthe
- syntaxofàPACKä.
- Ç
- üSCALARTYPESÇ
- ============
-
- Bydefinition,thescalartypeisanarrayofdefinedvalues
- thisvariabletypemayassume.
-
- Forexample,theàINTEGERÇtypeisascalartype,theàCHARÇ
- typeisonealso.TypeàREALäÇhowever,isnot.
-
- Theprogrammermaydefinehis/herownscalartypes.Eachnew
- typehasanidentifierandisdefinedbyalistofvaluesthat
- variablemaycarry.
-
- Example:àTYPEÇcivilstatus=(bachelor,married,divorced,
- separated,widow);
-
- Andwemaythendefineavariableofthistype:
-
- àVARÇalain:civilstatus;
-
- Thealainvariablemaynowcarryanyvalueofthecivilstatus
- type,beitbachelor,married,divorced,separated,orwidow.
-
- Example:alain:=bachelor;
-
- Thevaluescontainedinthelistdefiningascalartype
- variablearenotcharacterstrings.Itisimpossibletoreador
- printthem.However,thesevaluesmayinterveneinexpressionsand
- maybecompared.
-
- Itisimpossibletohavethesamevalueappearintwo
- differenttypedefinitions.
-
- Example:àTYPEÇfirstnames=(julie,serge,alain,denise);
- fabrics=(cotton,wool,serge);
-
- Thescalartypescreatedbytheprogrammerareusedtoclose
- thegapbetweenprogrammingandhumanlanguages.
-
- Untilthen,takecareandhappyprogramming!
-
-